package com.movefeng.hexoblogadmin.controller;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.movefeng.hexoblogadmin.service.CommentService;
import com.movefeng.hexoblogadmin.vo.CommentVO;
import com.movefeng.hexoblogadmin.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 评论相关Controller
 *
 * @author csp1999
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param searchParam
     * @return
     */
    @PostMapping("list")
    public Result commentList(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize, @RequestBody(required = false) Map<String, Object> searchParam){

        PageHelper.startPage(pageNum, pageSize);
        if (searchParam == null) {
            searchParam = new HashMap<>();
        }

        Page<CommentVO> page = commentService.queryCommentList(searchParam);
        PageInfo<CommentVO> pageInfo = new PageInfo<>(page);

        return new Result<>(Result.Code.SUCCESS, pageInfo);
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @PostMapping("delete")
    public Result commentDelete(@RequestParam("id") String id){

        System.out.println("objectId= "+id);
        AVOSCloud.useAVCloudCN();
        AVOSCloud.initialize("xxxx",
                "xxxx",
                "xxxxx");



        AVObject todo = AVObject.createWithoutData("Comment", id);
        try {
            todo.delete();
        } catch (AVException e) {
            e.printStackTrace();
        }

        commentService.deleteComment(id);


        return new Result<>(Result.Code.SUCCESS, "删除成功!");
    }
}
